﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using Model;

namespace FG_Admin_DAL
{
    public class UsersDAL
    {
        FGSkiingEntities model = new FGSkiingEntities();
        /// <summary>
        /// 返回所有用户信息
        /// </summary>
        /// <returns></returns>
        public List<Users> GetUsers(int PageIndex,int PageSize)
        {
            return model.Users.OrderBy(p=>p.Users_ID).Skip((PageIndex-1)*PageSize).Take(PageSize).ToList();
        }
        /// <summary>
        /// 返回当前用户信息
        /// </summary>
        /// <param name="UsersID">当前用户编号</param>
        /// <returns></returns>
        public Users_Message GetUsers_Message(int UsersID)
        {
            return model.Users_Message.Where(p => p.Users_ID == UsersID).FirstOrDefault();
        }
        /// <summary>
        /// 用户的禁用和启用
        /// </summary>
        /// <param name="ss"></param>
        /// <returns></returns>
        public int Users_Forbidden(Users ss)
        {
            if (ss.Users_Start == 1)
            {
                ss.Users_Start = 0;
                model.Entry(ss).State = System.Data.EntityState.Modified;
            }
            else
            {
                ss.Users_Start = 1;
                model.Entry(ss).State = System.Data.EntityState.Modified;
            }
            return model.SaveChanges();
        }
        /// <summary>
        /// 返回当前雪场的所有评论
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="SnowPack_ID">雪场编号</param>
        /// <returns></returns>
        public List<Users_Comment> GetUsers_Comment(int PageIndex, int PageSize, int SnowPack_ID)
        {
            return model.Users_Comment.Where(p => p.SnowPack_ID == SnowPack_ID).OrderBy(p => p.Users_Comment_ID).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
        }
        /// <summary>
        /// 返回当前雪场的所有评论
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="UsersID">用户编号</param>
        /// <param name="SnowPack_ID">雪场编号</param>
        /// <returns></returns>
        public List<Users_Comment> GetUsers_Comment(int PageIndex, int PageSize, int SnowPack_ID,int UsersID)
        {
            return model.Users_Comment.Where(p =>p.SnowPack_ID == SnowPack_ID && p.Users_ID==UsersID).OrderBy(p=>p.Users_Comment_ID).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
        }
        /// <summary>
        /// 删除该评论
        /// </summary>
        /// <param name="Users_Comment_ID">评论编号</param>
        /// <returns></returns>
        public int Delete_Comment(int Users_Comment_ID)
        {
            model.Users_Comment.Remove(model.Users_Comment.Where(p => p.Users_Comment_ID == Users_Comment_ID).FirstOrDefault());
            return model.SaveChanges();
        }
        /// <summary>
        /// 批量删除评论
        /// </summary>
        /// <param name="str">包含评论编号的字符串</param>
        /// <returns></returns>
        public int Delete_Comment(string str)
        {
            string[] list = str.Split(',');
            foreach (var i in list)
            {
                if (i != null || i != "")
                {
                    model.Users_Comment.Remove(model.Users_Comment.Where(p => p.Users_Comment_ID == int.Parse(i)).FirstOrDefault());
                }
            }
            return model.SaveChanges();
        }
    }
}
